/**
 * 
 */
package cn.suse.android;

import java.util.List;

import javax.security.auth.PrivateCredentialPermission;

import org.w3c.dom.Text;

import cn.suse.android.dialog.LoadingDialog;
import cn.suse.android.entity.Repaste;
import cn.suse.android.entity.RepasteDetail;
import cn.suse.android.logic.RepasteDetailParse;
import cn.suse.android.net.AppHelper;
import cn.suse.android.share.Constant;
import cn.suse.android.share.MainApplication;
import cn.suse.android.utils.MessageBoxUtils;
import cn.suse.android.utils.TimeUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author liubin
 * Create DateTime:Oct 19, 2012 2:57:47 PM
 * Description:
 * @Version: 1.0.0
 */
public class RepasteDetailActivity extends BaseActivity {

	private TextView repaste_detail_title;
	private TextView repaste_detail_author;
	private TextView repaste_detail_date;
	private WebView repaste_detail_webview;
	private Repaste repaste;
	/* (non-Javadoc)
	 * @see cn.suse.android.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repaste_detail);
		repaste = (Repaste) getIntent().getSerializableExtra("repaste");
		initFrameView();
//		new NewTask(RepasteDetailActivity.this,R.string.load_ing,R.string.load_error).execute();
		loadData();
	}

	private void initFrameView() {
		// TODO Auto-generated method stub
		repaste_detail_author = (TextView) findViewById(R.id.repaste_detail_author);
		repaste_detail_author.setOnClickListener(listener);
		repaste_detail_author.setTag(repaste.getUid());
		repaste_detail_date = (TextView)findViewById(R.id.repaste_detail_date);
		repaste_detail_title = (TextView) findViewById(R.id.repaste_detail_title);
		repaste_detail_webview = (WebView) findViewById(R.id.repaste_detail_webview);
		repaste_detail_author.setText(repaste.getFirst_name());
		repaste_detail_date.setText(TimeUtils.friendly_time(repaste.getCtime()));
	}
	private Handler repasteDetailHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			RepasteDetail repasteDetail = (RepasteDetail) msg.obj;
			if(repasteDetail == null){
				MessageBoxUtils.showMessage(RepasteDetailActivity.this, R.string.load_error);
				return ;
			}
			repaste_detail_title.setText(repasteDetail.getTitle());
			repaste_detail_webview.loadDataWithBaseURL(null, repasteDetail.getContent(), "text/html", "utf-8", null);
		};
	};
	private View.OnClickListener listener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String userId = (String) v.getTag();
			Intent intent = new Intent(RepasteDetailActivity.this,FriendActivity.class);
			intent.putExtra("userId", userId);
			startActivity(intent);
		}
	};
	private void loadData(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("uid", repaste.getUid());
				bundle.putString("urpid", repaste.getUrpid());
				String json = AppHelper.getFriendActive(MainActivity.instance.kaixin, RepasteDetailActivity.this, RepasteDetail.PATH_STRING, bundle, "GET");
				List<RepasteDetail> lists = new RepasteDetailParse().parseData(json);
				Message message = new Message();
				message.obj=lists == null?null:lists.get(0);
				repasteDetailHandler.sendMessage(message);
			}
		}).start();
	}
}
